package com.easy.custompasswordedit.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;

import com.easy.custompasswordedit.R;

/**
 * Created by Sigthp-P-Department on 2018/5/2.
 */

public class CustomPassWrodEditView extends android.support.v7.widget.AppCompatEditText {
    private final Context mContext;
    private float mFrameThickness;
    private int mFrameColor= Color.parseColor("#d1d2d6");;
    private float mPassWrodRidus;
    private int mPassWordColor= Color.parseColor("#d1d2d6");;
    public static int mPassWrodNumber=6;
    private Paint mPaint;
    private int mWidth;
    private int mHeight;
    private float mVerticalLineThickness;
    private int mPasswordItemWidth;

    public CustomPassWrodEditView(Context context) {
        this(context,null);
    }

    public CustomPassWrodEditView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomPassWrodEditView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;
        initAttrs(attrs);
        initPaint();
        // 设置输入模式是密码
        setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
        // 不显示光标
        setCursorVisible(false);
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CustomPassWrodEditView);
        mFrameThickness = typedArray.getDimension(R.styleable.CustomPassWrodEditView_FrameThickness,dip2px(2));
        mVerticalLineThickness = typedArray.getDimension(R.styleable.CustomPassWrodEditView_VerticalLineThickness,dip2px(1));
        mFrameColor = typedArray.getColor(R.styleable.CustomPassWrodEditView_FrameColor,mFrameColor);
        mPassWrodRidus = typedArray.getDimension(R.styleable.CustomPassWrodEditView_FrameRidus,dip2px(2));
        mPassWordColor = typedArray.getColor(R.styleable.CustomPassWrodEditView_PassWordColor,mPassWordColor);
        mPassWrodNumber = typedArray.getInt(R.styleable.CustomPassWrodEditView_PassWrodNumber, mPassWrodNumber);
        typedArray.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mWidth = getWidth();
        mHeight = getHeight();
        drawBGFrame(canvas);
        drawVerticalLine(canvas);
        drawPassWrod(canvas);
    }

    /**
     * 画密码
     * @param canvas
     */
    private void drawPassWrod(Canvas canvas) {
        mPaint.setColor(mPassWordColor);
        mPaint.setStyle(Paint.Style.FILL);
        int passwordLength = getText().length();

        for(int x=0;x<passwordLength;x++){
            int cx = (int) (x * mVerticalLineThickness + x * mPasswordItemWidth + mPasswordItemWidth / 2 + mFrameThickness);
            canvas.drawCircle(cx,mHeight/2,mPassWrodRidus,mPaint);
        }
    }

    /**
     * 画竖线
     * @param canvas
     */
    private void drawVerticalLine(Canvas canvas) {
        mPaint.setColor(mFrameColor);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(mVerticalLineThickness);
        int passwordWidth = (int) (getWidth() - (mPassWrodNumber - 1) * mVerticalLineThickness);
        mPasswordItemWidth = passwordWidth / mPassWrodNumber;
        for (int i = 1; i < mPassWrodNumber ; i++) {
            int startX =0;
            startX = (int) ((i ) * mVerticalLineThickness + (i) * mPasswordItemWidth );
            canvas.drawLine(startX, 0, startX, getHeight() , mPaint);
        }

    }

    /**
     * 绘制背景框
     */
    private void drawBGFrame(Canvas canvas) {
        mPaint.setColor(mFrameColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mFrameThickness);
        RectF rectF=new RectF(0,0, mWidth, mHeight);
        canvas.drawRect(rectF, mPaint);
    }

    /**
     * 根据手机分辨率从DP转成PX
     * @param dpValue
     * @return
     */
    public float dip2px(float dpValue) {
        float scale = mContext.getResources().getDisplayMetrics().density;
        return (float) (dpValue * scale + 0.5f);
    }

}
